package com.base.common;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;
import net.sf.json.processors.PropertyNameProcessor;
import net.sf.json.util.CycleDetectionStrategy;
import net.sf.json.util.PropertyFilter;

public class JsonConfigExt extends JsonConfig {

	private Class cls;

	public Class getCls() {
		return cls;
	}

	public void setCls(Class cls) {
		this.cls = cls;
	}
	
	public JsonConfigExt(Class cls) {
		this.cls =cls;
		   this.registerJsonPropertyNameProcessor(this.cls, new PropertyNameProcessor(){ 
				public String processPropertyName(Class obj, String name) { 
			        if(name.equalsIgnoreCase("modulePid") || name.equalsIgnoreCase("deptParentId") || name.equalsIgnoreCase("positionParentId")){
			        	return "_parentId"; 
			        }
			       return name; 
			} 
			});
		   this.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
		   this.registerJsonValueProcessor(Date.class, new JsonValueProcessor() {
			public Object processObjectValue(String name, Object value, JsonConfig cfg) {
				if (!(value instanceof Date))
					return null;
				DateFormat dateFormat = new SimpleDateFormat(CommonConstants.DATE_FORMAT);
				String dateStr = dateFormat.format(value);
				return dateStr;
			}

			public Object processArrayValue(Object arg0, JsonConfig arg1) {
				return null;
			}
		});
		   this.setJsonPropertyFilter(new PropertyFilter() {
			public boolean apply(Object obj, String name, Object value) {
				return value == null;
			}
		});
	}
	@Override
	public void setCycleDetectionStrategy(
			CycleDetectionStrategy cycleDetectionStrategy) {
		   super.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
	}
	@Override
	public void registerJsonPropertyNameProcessor(Class target,
			PropertyNameProcessor propertyNameProcessor) {
		   super.registerJsonPropertyNameProcessor(this.cls, new PropertyNameProcessor(){ 
		    public String processPropertyName(Class obj, String name) { 
			   if(name.equalsIgnoreCase("modulePid") || name.equalsIgnoreCase("deptParentId") || name.equalsIgnoreCase("positionParentId")){
		        	return "_parentId"; 
		       }
		       return name; 
		} 
		});
	}
	
	@Override
	public void registerJsonValueProcessor(Class beanClass,JsonValueProcessor jsonValueProcessor) {
		super.registerJsonValueProcessor(Date.class, new JsonValueProcessor() {
			public Object processObjectValue(String name, Object value, JsonConfig cfg) {
				if (!(value instanceof Date))
					return null;
				DateFormat dateFormat = new SimpleDateFormat(CommonConstants.DATE_FORMAT);
				String dateStr = dateFormat.format(value);
				return dateStr;
			}

			public Object processArrayValue(Object arg0, JsonConfig arg1) {
				return null;
			}
		});
	}
	
	@Override
	public void setJsonPropertyFilter(PropertyFilter jsonPropertyFilter) {
		super.setJsonPropertyFilter(new PropertyFilter() {
			public boolean apply(Object obj, String name, Object value) {
				return value == null;
			}
		});
	}
}
